//
//  HWGameCommunityDetailHeader.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/21.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameCommunityDetailHeader: UIView {
    var shareImage:UIImageView!
    var shareLabel:UILabel!
    var shareButton:UIButton!
    var commentImage:UIImageView!
    var commentLabel:UILabel!
    var commentButton:UIButton!
    var zanImage:UIImageView!
    var zanLabel:UILabel!
    var zanButton:UIButton!
    var lineView:UIView!
    var allLabel:UILabel!
    var imagesArr:[UIImageView] = []
    var imagesButtonArr:[UIButton] = []
    var videoImage:UIImageView!
    var playImageView:UIImageView!
    var playButton:UIButton!
    @objc var model:HWGameCommunityDetailModel? {
        didSet {
            zanLabel.text = model?.good
            commentLabel.text = model?.pinglun
            shareLabel.text = model?.look
            if (model?.pinglun.count ?? 0) > 0{
                if model?.pinglun != "0" {
                    allLabel.text = "全部回复"
                } else {
                    allLabel.text = ""
                }
            } else {
                allLabel.text = ""
            }
//            if model?.is_pinglun == true {
//                commentImage.image = UIImage(named: "communityCommentSelect")
//                commentLabel.textColor = .colorWithHexString(hexString: "#FA8C08")
//            } else {
//                commentImage.image = UIImage(named: "communityCommentNormal")
//                commentLabel.textColor = .colorWithHexString(hexString: "#888888")
//            }
            playImageView = nil
            videoImage = nil
            playButton = nil
            if model?.is_good == true {
                zanImage.image = UIImage(named: "communityZanSelect")
                zanLabel.textColor = .colorWithHexString(hexString: "#FA8C08")
            } else {
                zanImage.image = UIImage(named: "communityZanNormal")
                zanLabel.textColor = .colorWithHexString(hexString: "#888888")
            }
            
            for i in imagesArr {
                i.removeFromSuperview()
            }
            
            imagesArr = []
            
            for b in imagesButtonArr {
                b.removeFromSuperview()
            }
            
            imagesButtonArr = []
            
            if (model?.video.count ?? 0) > 0  {
                videoImage = UIImageView()
                videoImage.contentMode = .scaleAspectFill
                videoImage.layer.masksToBounds = true
                videoImage.layer.cornerRadius = 4*widthScale
                videoImage.kf.setImage(with: URL(string: model?.image[0] ?? ""))
                addSubview(videoImage)
                
                playImageView = UIImageView()
                playImageView.image = UIImage(named: "playVideo")
                videoImage.addSubview(playImageView)
                
                playButton = UIButton(type: .custom)
                playButton.addTarget(self, action: #selector(playButtonClick(_:)), for: .touchUpInside)
                addSubview(playButton)
                
                videoImage.snp.makeConstraints { make in
                    make.top.equalTo(20*widthScale)
                    make.left.equalTo(18*widthScale)
                    make.right.equalTo(-18*widthScale)
                    make.height.equalTo(163*widthScale)
                }
                
                playImageView.snp.makeConstraints { make in
                    make.center.equalToSuperview()
                }
                
                playButton.snp.makeConstraints { make in
                    make.edges.equalTo(videoImage)
                }
                
                self.shareImage.snp.remakeConstraints { make in
                    make.left.equalTo(18*widthScale)
                    make.width.height.equalTo(14*widthScale)
                    make.top.equalTo(videoImage.snp.bottom).offset(10*widthScale)
                }
                
            } else {
                if (model?.image.count ?? 0) > 0 {
                    var tempImageView:UIImageView!
                    for idx in 0..<(model?.image ?? []).count  {
                        let imageView = UIImageView()
                        imageView.contentMode = .scaleAspectFill
                        imageView.layer.masksToBounds = true
                        imageView.layer.cornerRadius = 4*widthScale
                        imageView.kf.setImage(with: URL(string: model?.image[idx] ?? ""))
                        addSubview(imageView)
                        
                        let btn = UIButton(type: .custom)
                        btn.tag = idx
                        btn.addTarget(self, action: #selector(imageButtonClick(_:)), for: .touchUpInside)
                        addSubview(btn)
                        
                        if tempImageView != nil{
                            if idx % 3 == 0 {
                                imageView.snp.makeConstraints { make in
                                    make.left.equalTo(18*widthScale)
                                    make.top.equalTo(tempImageView.snp.bottom).offset(10*widthScale)
                                    make.width.height.equalTo(109*widthScale)
                                }
                            } else {
                                imageView.snp.makeConstraints { make in
                                    make.left.equalTo(tempImageView.snp.right).offset(10*widthScale)
                                    make.top.equalTo(tempImageView)
                                    make.width.height.equalTo(109*widthScale)
                                }
                            }
                            
                        } else {
                            imageView.snp.makeConstraints { make in
                                make.left.equalTo(18*widthScale)
                                make.top.equalTo(20*widthScale)
                                make.width.height.equalTo(109*widthScale)
                            }
                        }
                        tempImageView = imageView
                        imagesArr.append(imageView)
                        
                        btn.snp.makeConstraints { make in
                            make.edges.equalTo(imageView)
                        }
                        
                        imagesButtonArr.append(btn)
                    }
                    
                    self.shareImage.snp.remakeConstraints { make in
                        make.left.equalTo(18*widthScale)
                        make.width.height.equalTo(14*widthScale)
                        make.top.equalTo(imagesArr.last!.snp.bottom).offset(10*widthScale)
                    }
                } else {
                    self.shareImage.snp.remakeConstraints { make in
                        make.left.equalTo(18*widthScale)
                        make.top.equalTo(20*widthScale)
                        make.width.height.equalTo(14*widthScale)
                    }
                }
            }
        }
    }
    
    @objc var zanButtonClick:((_:HWGameCommunityDetailModel)->())?
    @objc var commentButtonClick:((_:HWGameCommunityDetailModel)->())?
    @objc var playButtonClick:((_:HWGameCommunityDetailModel)->())?
    @objc var imageButtonClick:((_:HWGameCommunityDetailModel,_:Int)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension HWGameCommunityDetailHeader {
    private func initUI() -> Void {
        shareImage = UIImageView()
        shareImage.image = UIImage(named: "chakankaiyang")
        addSubview(shareImage)
        
        shareLabel = HWLabel.createLabel(title: "", color: "#888888", font: 13*widthScale)
        addSubview(shareLabel)
        
        shareButton = UIButton(type: .custom)
        shareButton.addTarget(self, action: #selector(shareButtonClick(_:)), for: .touchUpInside)
        addSubview(shareButton)
        
        commentImage = UIImageView()
        commentImage.image = UIImage(named: "communityCommentNormal")
        addSubview(commentImage)
        
        commentLabel = HWLabel.createLabel(title: "0", color: "#888888", font: 13*widthScale)
        addSubview(commentLabel)
        
        commentButton = UIButton(type: .custom)
        commentButton.addTarget(self, action: #selector(commentButtonClick(_:)), for: .touchUpInside)
        addSubview(commentButton)
        
        zanImage = UIImageView()
        zanImage.image = UIImage(named: "communityZanNormal")
        addSubview(zanImage)
        
        zanLabel = HWLabel.createLabel(title: "0", color: "#888888", font: 13*widthScale)
        addSubview(zanLabel)
        
        zanButton = UIButton(type: .custom)
        zanButton.addTarget(self, action: #selector(zanButtonClick(_:)), for: .touchUpInside)
        addSubview(zanButton)
        
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "#F7F7F7")
        addSubview(lineView)
        
        allLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 14*widthScale)
        addSubview(allLabel)
    }
    
    private func initFrame() -> Void {
        shareImage.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.top.equalTo(20*widthScale)
            make.width.height.equalTo(14*widthScale)
        }
        
        shareLabel.snp.makeConstraints { make in
            make.left.equalTo(shareImage.snp.right).offset(4*widthScale)
            make.height.equalTo(18*widthScale)
            make.centerY.equalTo(shareImage)
        }
        
        shareButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(shareImage)
            make.right.equalTo(shareLabel)
        }
        
        commentImage.snp.makeConstraints { make in
            make.left.equalTo(shareLabel.snp.right).offset(36*widthScale)
            make.centerY.equalTo(shareLabel)
            make.width.height.equalTo(14*widthScale)
        }
        
        commentLabel.snp.makeConstraints { make in
            make.left.equalTo(commentImage.snp.right).offset(4*widthScale)
            make.height.equalTo(18*widthScale)
            make.centerY.equalTo(commentImage)
        }
        
        commentButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(commentImage)
            make.right.equalTo(commentLabel)
        }
        
        zanImage.snp.makeConstraints { make in
            make.left.equalTo(commentLabel.snp.right).offset(36*widthScale)
            make.centerY.equalTo(shareLabel)
            make.width.height.equalTo(14*widthScale)
        }
        
        zanLabel.snp.makeConstraints { make in
            make.left.equalTo(zanImage.snp.right).offset(4*widthScale)
            make.height.equalTo(18*widthScale)
            make.centerY.equalTo(zanImage)
        }
        
        zanButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(zanImage)
            make.right.equalTo(zanLabel)
        }
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(shareImage.snp.bottom).offset(20*widthScale)
            make.height.equalTo(9*widthScale)
            make.left.equalTo(0)
            make.right.equalTo(0)
        }
        
        allLabel.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.height.equalTo(20*widthScale)
            make.top.equalTo(lineView.snp.bottom).offset(18*widthScale)
        }
    }
}

extension HWGameCommunityDetailHeader {
    
    @objc func shareButtonClick(_:UIButton) -> Void {
        
    }
    
    @objc func commentButtonClick(_:UIButton) -> Void {
        self.commentButtonClick?(model ?? HWGameCommunityDetailModel())
    }
    
    @objc func zanButtonClick(_:UIButton) -> Void {
        self.zanButtonClick?(model ?? HWGameCommunityDetailModel())
    }
    
    @objc func playButtonClick(_:UIButton) -> Void {
        self.playButtonClick?(model ?? HWGameCommunityDetailModel())
    }
    
    @objc func imageButtonClick(_ button:UIButton) -> Void {
        self.imageButtonClick?(model ?? HWGameCommunityDetailModel(),button.tag)
    }
}
